package com.chixing.controller;

import com.chixing.pojo.Customer;
import com.chixing.service.CustomerService;
import com.chixing.util.RSAUtils;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    //http://localhost:8080/zyx/customer/1001
    @GetMapping("/customer/{custId}")
    @ResponseBody //返回json数据
    public ServerResponse getById(@PathVariable("custId") Integer custId) {
        System.out.println("custId:" + custId);
        return customerService.getById(custId);
    }

    @PostMapping("customer")
    public ModelAndView save(Customer customer) {
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse1 = customerService.save(customer);

        if (serverResponse1.getCode() == 200) {
            mav.setViewName("customer/login");
        } else {
            mav.addObject("addMessage", "添加失败");
            mav.setViewName("customer/register");

        }
        return mav;

    }

    @PostMapping("login")
    public ModelAndView login(Long custTelno, String consumerPassword, HttpSession session) {
        System.out.println("session id:" + session.getId());
        ModelAndView mav = new ModelAndView();
        ServerResponse login = customerService.login(custTelno, consumerPassword);
        Customer customer = (Customer) login.getData();
        if (login.getCode()==200 ){ // 用户找到了，也就是 用户名和密码是正确的，跳转到首页index.jsp , 首页中显示用户名
            session.setAttribute("loginCustomer", customer); // 绑定登录用户 到session中
            System.out.println("++++++++++++++++++++++++++;" + customer);
            session.setAttribute("custId",customer.getCustId());
            mav.setViewName("index");
        } else  { //  用户名和密码是错误的，跳转到login.jsp ，提示 用户名或密码错误，请重新输入
            mav.addObject("loginMsg","用户名或密码错误，请重新输入");
            mav.setViewName("customer/login"); //  /customer/login.jsp
        }
        return mav;
    }


    //退出
    @GetMapping("cust/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("loginCustomer");// 退出： 移除session中的登录用户

        return "redirect:/index.jsp";  // 重定向(绝对路径)
    }

    @PostMapping("register")
    public ModelAndView regist(@RequestParam Long custTelno,
                               @RequestParam String consumerPassword ,HttpSession session) {

        System.out.println("前端返回的密文:"+consumerPassword);
        System.out.println("session id:" + session.getId());
        ModelAndView mav = new ModelAndView();

        ServerResponse regist = customerService.regist(custTelno);
        if(regist.getData().equals("existence")){
            System.out.println("存在");
            mav.addObject("loginMsg","手机号码已被注册过请返回登录");
            mav.setViewName("customer/register");
        }else{
            customerService.save( custTelno , consumerPassword);
            mav.addObject("custTelno",custTelno);
            mav.setViewName("customer/login");

        }
        return mav;
    }


    @GetMapping("publickey")
    @ResponseBody
    public ServerResponse resregister() {
        return customerService.getPublicKey();
    }


    @GetMapping("/encrypted")
    @ResponseBody
    public ServerResponse password(@RequestParam("encrypted") String encrypted) {
        System.out.println("密文: ");
        System.out.println(encrypted);

        String privateKey = customerService.getPrivateKey();

        try {
            System.out.println("解密中.....");
            String decryptedBytes = RSAUtils.privateDecrypt(encrypted, RSAUtils.getPrivateKey(privateKey));
            System.out.println("解密后：" + decryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}


